package com.datasure.application.service.impl;

import com.datasure.application.res.assembler.DeviceEntityAssembler;
import com.datasure.application.res.dto.TargetDeviceShowDTO;
import com.datasure.application.service.TargetShowService;
import com.datasure.domain.entity.CloudPlatform;
import com.datasure.domain.entity.Device;
import com.datasure.external.CloudPlatformExternalService;
import com.datasure.repository.CloudPlatformRepository;
import com.datasure.repository.DeviceRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;

/**
 * @author zhangxiaolong
 * @ClassName TargetShowServiceImpl.java
 * @Description
 * @createTime 2022年02月09日 14:28:00
 */
public class TargetShowServiceImpl implements TargetShowService {

    private DeviceRepository deviceRepository;
    private CloudPlatformRepository cloudPlatformRepository;
    private CloudPlatformExternalService cloudPlatformExternalService;

    private DeviceEntityAssembler deviceEntityAssembler = DeviceEntityAssembler.INSTANCE;

    /**
     * 获取目标设备列表
     * @param state
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public List<TargetDeviceShowDTO> showList(Integer state) throws ExecutionException, InterruptedException {

        //获取所有云平台信息，这块编排逻辑，放在application层中实现
        List<Device> allCloudDevice = new ArrayList<>();
        List<CloudPlatform> cloudPlatformList = cloudPlatformRepository.findAll();
        for(CloudPlatform cloudPlatform: cloudPlatformList){
            // TODO: 2022/2/9 替换为专用线程池
            Callable<List<Device>> callable = new Callable<List<Device>>() {
                @Override
                public List<Device> call() throws Exception {
                    return cloudPlatformExternalService.findDevice(cloudPlatform);
                }
            };
            FutureTask<List<Device>> ft = new FutureTask<>(callable);
            new Thread(ft).start();

            allCloudDevice.addAll(ft.get());
        }

        //获取已安装目标端过的云主机
        List<Device> cloudTargetList = deviceRepository.getCloudTargetList();

        //获取所有的目标端设备
        List<Device> allTargetList = deviceRepository.getAllTargetList();

        /**
         * 组装返回信息
         */
        List<TargetDeviceShowDTO> targetDeviceShowDTOList = allTargetList.stream()
                .filter(device -> device.targetDeviceFilter(state))
                .map(device -> {
                    TargetDeviceShowDTO targetDeviceShowDTO = deviceEntityAssembler.toTagetDeviceShowDTO(device);

                    //设置目标设备类型
                    Integer targetType = device.getTargetType();
                    //targetDeviceShowDTO.setTargetType(targetType);

                    //设备目标设备操作状态
                    Integer targetState = device.getTargetState();
                    //targetDeviceShowDTO.setTargetState(targetState);

                    //...... 目标设备对象自己的业务逻辑

                    return targetDeviceShowDTO;
                }).collect(Collectors.toList());

        return targetDeviceShowDTOList;
    }
}
